
OCAML = ocaml-3.12.0+dev
OCAMLFILE = $(OCAML).touch
BITSTRING = ocaml-bitstring-2.0.0
BITSTRINGFILE = $(BITSTRING).touch
FREETYPE = freetype-2.3.11
CAIRO = cairo-1.8.10
PIXMAN = pixman-0.18.0
PWD = $(shell pwd)
OCAMLCMDS = "ocamlc=$(PWD)/custom/bin/ocamlc.opt ocamlopt=$(PWD)/custom/bin/ocamlopt.opt camlp4o=$(PWD)/custom/bin/camlp4o"
SAFETOOLSPREFIX = $(shell echo $(TOOLSPREFIX) | sed -e 's/\//\\\//g')

all: ocamlopt.opt  bitstring

ocamlopt.opt: $(OCAMLFILE)
	cp -f configure.ocaml $(OCAML)/configure
	cd $(OCAML) && chmod -R 777 *
	cp -f boot-c-parts.sh $(OCAML)/build/
	cp -f install.sh $(OCAML)/build/
	cd $(OCAML) && patch -Np0 -i ../ocaml.dev.patch
	cd $(OCAML) && ./configure -cc "gcc -m32" -as "as --32" -asflags "@ASFLAGS@" -aspp "gcc -m32 -c" -prefix $(PWD)/custom -no-curses -no-pthread -no-tk
#	cd $(OCAML) && ./configure -cc "gcc -m32" -as "as --32 --size-check=warning" -aspp "gcc -m32 -c" -prefix $(PWD)/custom -no-curses -no-pthread -no-tk
#	cp -f Compflags $(OCAML)/stdlib/
	$(MAKE) -C $(OCAML) opt.opt
	$(MAKE) -C $(OCAML) install
	cp custom/bin/ocamlopt.opt ./ocamlopt.opt

oldstuff:
	# can't build a 32-bit compiler yet, so this patch breaks their build system
	#cd trunk && patch -Np0 -i ../ocaml.dev.patch
	mv trunk $(OCAML)
	# tarball from viewcvs doesn't include execute bits
	cd $(OCAML) && chmod -R 777 *
	# copy over modified boot-c-parts.sh
	cp -f boot-c-parts.sh $(OCAML)/build/
	cp -f install.sh $(OCAML)/build/
	# build host compiler tools
	cd $(OCAML) && ./configure -prefix $(PWD)/ocaml -no-curses -no-pthread -no-tk
	cd $(OCAML) && MAKE=$(MAKE) ./build/fastworld.sh
	cd $(OCAML) && MAKE=$(MAKE) ./build/install.sh
	# now overwrite with the snowflake 32-bit configuration
	cd $(OCAML) && cp -rf ../config.snowflake/* config/
	cd $(OCAML) && patch -Np0 -i ../ocaml.dev.patch
	# update some more config files...
	if [ ! -z "$(TOOLSPREFIX)" ]; then sed -i -e 's/^TOOLSPREFIX=/TOOLSPREFIX=$(SAFETOOLSPREFIX)\/i386-elf-/' $(OCAML)/config/Makefile; fi
	# fix macosx: doesn't handle --32
	if [ ! -z "$(TOOLSPREFIX)" ]; then \
		if [ ! -z "`grep -e 'macosx' $(OCAML)/config/config.sh`" ]; then \
			sed -i -e 's/i386-elf-as --32/i386-elf-as -arch i386/' $(OCAML)/config/Makefile; \
		fi; \
	fi
	cd $(OCAML) && ./build/mkconfig.sh && ./build/mkmyocamlbuild_config.sh
	# finally build our own ocamlopt.opt
	cd $(OCAML) && mkdir snowflake
	cd $(OCAML) && cp _build/ocamlopt snowflake/
	cd $(OCAML) && cp -r boot/ snowflake/boot/
	cd $(OCAML) && rm snowflake/boot/ocamlrun
	cd $(OCAML) && cp -f byterun/ocamlrun snowflake/boot/ocamlrun
	cd $(OCAML) && cp _build/boot/ocamlyacc snowflake/boot/ocamlyacc
	cd $(OCAML) && $(PWD)/ocaml/bin/ocamlbuild -build-dir snowflake stdlib/std_exit.cmx ocamlopt.opt
	# and copy it to the tools directory
	cp $(OCAML)/snowflake/ocamlopt.opt ./ocamlopt.opt

$(OCAMLFILE):
	tar zxf $(OCAML).tar.gz
	mv trunk $(OCAML)
	touch $(OCAMLFILE)

bitstring: $(BITSTRINGFILE) ocamlopt.opt
	cd $(BITSTRING) && cp -f ../Makefile.in.bitstring Makefile.in && cp -f ../configure.bitstring configure && PATH=../custom/bin:$$PATH$$ ./configure CC="gcc -m32"
	$(MAKE) -C $(BITSTRING) all install
	touch bitstring

$(BITSTRINGFILE):
	tar xzf $(BITSTRING).tar.gz
	touch $(BITSTRINGFILE)

freetype: custom/lib/libfreetype.a

custom/lib/libfreetype.a: $(FREETYPE)
	cd $(FREETYPE) && CC="gcc -m32" CFLAGS="-fno-builtin" \
		./configure --prefix=$(PWD)/custom \
		--disable-shared \
		--without-zlib \
		--without-old-mac-fonts \
		--without-fsspec \
		--without-fsref \
		--without-quickdraw-toolbox \
		--without-quickdraw-carbon \
		--without-ats \
		--without-pic
	cp -f modules.cfg $(FREETYPE)/
	cp -f ftsystem.c $(FREETYPE)/src/base/
	cp -f ftoption.h $(FREETYPE)/objs/
	cp -f $(FREETYPE)/builds/ansi/ansi.mk $(FREETYPE)/config.mk
	sed -i -e 's/:= cc/:= gcc -m32/' $(FREETYPE)/builds/compiler/ansi-cc.mk
	make -C $(FREETYPE) all
	cp -f $(FREETYPE)/objs/libfreetype.a custom/lib/
	cp -rf $(FREETYPE)/include custom/

$(FREETYPE):
	tar jxf $(FREETYPE).tar.bz2

cairo: custom/lib/libcairo.a

custom/lib/libcairo.a: $(CAIRO) custom/lib/libpixman-1.a
	cd $(CAIRO) && PKG_CONFIG_PATH=$(PWD)/custom/lib/pkgconfig CC="gcc -m32" CFLAGS="-fno-builtin" \
		./configure --prefix=$(PWD)/custom \
		--disable-xlib \
		--disable-xcb \
		--disable-png \
		--disable-directfb \
		--disable-ft \
		--disable-pthread \
		--disable-ps \
		--disable-pdf \
		--disable-svg \
		--enable-shared=no || true
	echo "#define CAIRO_NO_MUTEX 1" >> $(CAIRO)/config.h
	$(MAKE) -C $(CAIRO) all install

$(CAIRO):
	tar zxf $(CAIRO).tar.gz

pixman: custom/lib/libpixman-1.a

custom/lib/libpixman-1.a: $(PIXMAN)
	cd $(PIXMAN) && sed -i 's/pixman test/pixman/' Makefile.in && CC="gcc -m32" CFLAGS="-fno-builtin" ./configure --disable-shared --disable-mmx --disable-sse2 --prefix=$(PWD)/custom
	$(MAKE) -C $(PIXMAN) all install

$(PIXMAN):
	tar zxf $(PIXMAN).tar.gz

clean:
	rm -rf custom $(OCAML) $(OCAMLFILE) trunk
	rm -rf $(BITSTRING) $(BITSTRINGFILE) bitstring
	rm -rf $(CAIRO) $(PIXMAN) custom

cleanbitstring:
	rm -rf $(BITSTRING) $(BITSTRINGFILE) bitstring
